#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
int n;
const int N = 1e5 + 10;
int a[N], s[N];
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    int num = 0;
    for (int i = 0, j = -1; i < n; i++)
    {
        while (s[a[j]] < 2)
        {
            j++;
            s[a[j]]++;
        }
        s[a[i]]--;
        num = max(num, j - i);
    }
    printf("%d", num);
}